﻿using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TAB05AV12
    {
        public TAB05AV12()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VABFOLGE();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
           if(dwMaster==null)
            {
                RPAUF = "";
            }
           else
            {
                RPAUF = "V" + dwMaster["ABFREAUFKRS"].ToString() + dwMaster["ABFREAUFLFD"].ToString() + dwMaster["ABFREAUFPOS"].ToString();
            } 
        }
       
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 11)
                return;
            KRS = EXAUF.Substring(1, 2);
            LFD = EXAUF.Substring(3, 6);
            POS = EXAUF.Substring(9, 3);
            dwData = TBMain.NewRow();
            dwData["ABFMANDANT"] = app.Mandant;
            dwData["ABFAUFKRS"] = " AND ABFAUFKRS ='" + KRS + "'";
            dwData["ABFAUFLFD"] = " AND ABFAUFLFD ='" + LFD + "'";
            dwData["ABFAUFPOS"] = " AND ABFAUFPOS ='" + POS + "'";
            SResult = TMV.QABF14(dwData);
            MergeData(SResult, false);
            base.OnEXAUFChg(vlaue);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if(dwMaster==null)
            {
                MessageBox.Show("请选择要回修的步骤");
                return;
            }
            if (REWEIL.Trim().Length==0)
            {
                MessageBox.Show("请提供回修原因");
                return;
            }
            
            AB05RP(dwMaster);
            base.NewCommand(sender, e);
        }
        public void AB05RP(DataRow dwAB)
        {
            //根据当前的需求，创建细纱单，注意，这个最好直接执行，因为可能存在多个需求，要逐个创建，
            //比如AB纱
            try
            {
                KRS = "X"+dwAB["ABPAUFKRS"].ToString().Substring(1);
                PM["KRS"] = KRS;
                PM["LFD"] = dwAB["ABPAUFLFD"].ToString();
                PM["AUFART"] = "AM";
                PM["ORDERNUM"] = dwAB["ABKKDAUFTNR"].ToString();
                PM["DELDATE"] = dwAB["ABPLIEFDAT"];

             

                PM["QTYEND"] = QTY;//这句话，之前是否也是隐藏的》之前是有的 
                PM["CART"] = dwAB["ABPART"].ToString() + dwAB["ABPVAR"].ToString().Trim().PadRight(2) + dwAB["ABPFARBE"].ToString();
                POS = VSDDAT.SD22.GETNEXT("0022", KRS, dwAB["ABPAUFLFD"].ToString(), 1);
                POS = Helpers.Helper.Right(POS, 3);
                dwAB["ABFREAUFKRS"] = KRS;
                dwAB["ABFREAUFLFD"] = dwAB["ABPAUFLFD"].ToString();
                dwAB["ABFREAUFPOS"] = POS;
                dwAB["ABFREWEIL"] = REWEIL.PadRight(2);
                PM["ORDERNUM"] = KRS + dwAB["ABPAUFLFD"].ToString() + POS + "AM ";
                PM["COLOR"] = "AM ";
                PM["AUFTYP"] = "V";
                PM["ARTSTAGE"] = "Z";
                //PM["VKAUF"] = pm["ORDERNUM"];
                PM["VKPAUF"] = dwAB["ABKKDAUFTNR"].ToString();
                PM["VKPQTY"] = 0;//若按你
                PM["VKAUF"] = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABFAFOLGE"].ToString() + "V";//若按你
                TMV.VPR.NEWPLAN(PM);

                MessageInfo msg = new MessageInfo();
                msg.Sender = RUNID;
                msg.MSGTYPE = "AB05RP";
                msg.ACTION = "AB05RP";
                msg.AUFTYP = "V";
                msg.WAKZ = dwAB["ABPWAKZ"].ToString();
                msg.ART = dwAB["ABPART"].ToString();
                msg.VAR = dwAB["ABPVAR"].ToString().Trim().PadRight(2);
                msg.FARBE = dwAB["ABPFARBE"].ToString();
                msg.KRS = dwAB["ABPAUFKRS"].ToString();
                msg.LFD = dwAB["ABPAUFLFD"].ToString();
                msg.POS = dwAB["ABPAUFPOS"].ToString();
                msg.P1 = dwAB["ABFAFOLGE"].ToString();
                msg.P2 = REWEIL.PadRight(2);
                msg.ORDER = (string)PM["ORDERNUM"];
                msg.Where = app.User;
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
